package com.government.web.controller.company;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;
import com.government.common.annotation.Anonymous;
import com.government.common.annotation.RepeatSubmit;
import com.government.company.bo.CompanyExcel;
import com.government.company.bo.QccCompanyExcel;
import com.government.company.domain.CompanyAccs;
import com.government.company.domain.CompanyPatent;
import com.government.company.domain.CompanyStd;
import com.government.company.vo.CompanyVo;
import com.government.item.domain.ExcelDetail;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.government.common.annotation.Log;
import com.government.common.core.controller.BaseController;
import com.government.common.core.domain.AjaxResult;
import com.government.common.enums.BusinessType;
import com.government.company.domain.Company;
import com.government.company.service.ICompanyService;
import com.government.common.utils.poi.ExcelUtil;
import com.government.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 企业信息
 *
 * @author system
 * @date 2023-09-15
 */
@RestController
@RequestMapping("/company/company")
public class CompanyController extends BaseController {
    @Autowired
    private ICompanyService companyService;

    /**
     * 查询公司列表
     */
    @PreAuthorize("@ss.hasPermi('company:company:list')")
    @GetMapping("/list")
    public TableDataInfo list(Company company) {
        startPage();
        if (CollUtil.isNotEmpty(company.getParkAreaIds())) {
            company.setParkAreaId(String.join(",", company.getParkAreaIds()));
        }
        List<Company> list = companyService.selectCompanyList(company);
        return getDataTable(list);
    }

    /**
     * 导出公司列表
     */
    @PreAuthorize("@ss.hasPermi('company:company:export')")
    @Log(title = "公司", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Company company) {
        List<Company> list = companyService.selectCompanyList(company);
        ExcelUtil<Company> util = new ExcelUtil<Company>(Company.class);
        util.exportExcel(response, list, "公司数据");
    }

    /**
     * 获取公司详细信息
     */
    @PreAuthorize("@ss.hasPermi('company:company:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(companyService.selectCompanyById(id));
    }

    /**
     * 新增公司
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('company:company:add')")
    @Log(title = "公司", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Company company) {
        return toAjax(companyService.insertCompany(company));
    }

    /**
     * 修改公司
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('company:company:edit')")
    @Log(title = "公司", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Company company) {
        return toAjax(companyService.updateCompany(company));
    }

    /**
     * 删除公司
     */
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('company:company:remove')")
    @Log(title = "公司", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(companyService.deleteCompanyById(id));
    }

    /**
     * 查询公司列表（联想匹配）
     */
    @GetMapping("/searchCompanyList")
    public AjaxResult searchCompanyList(Company company) {
        List<Company> list = companyService.searchCompanyList(company);
        return AjaxResult.success(list);
    }

//    /**
//     * 查询公司详情
//     */
//    @GetMapping("/selectCompanyInfo")
//    public AjaxResult selectCompanyInfo(Long id)
//    {
//        CompanyVo result = companyService.selectCompanyInfo(id);
//        return AjaxResult.success(result);
//    }

    @Anonymous
    @GetMapping(value = "/listCompanyPatent")
    public AjaxResult listCompanyPatent(Long companyId) {
        return success(companyService.getCompanyPatentList(companyId));
    }

    @Anonymous
    @GetMapping(value = "/listCompanyAccs")
    public AjaxResult listCompanyAccs(Long companyId) {
        return success(companyService.getCompanyAccsList(companyId));
    }

    @Anonymous
    @GetMapping(value = "/getCompanyPatent")
    public AjaxResult getCompanyPatent(Long id) {
        return success(companyService.getCompanyPatent(id));
    }

    @GetMapping(value = "/getCompanyAccs")
    public AjaxResult getCompanyAccs(Long id) {
        return success(companyService.getCompanyAccs(id));
    }

    @PostMapping(value = "/addCompanyPatent")
    public AjaxResult addCompanyPatent(@RequestBody CompanyPatent companyPatent) {
        companyService.insertCompanyPatent(companyPatent);
        return success();
    }

    @PostMapping(value = "/addCompanyAccs")
    public AjaxResult addCompanyAccs(@RequestBody CompanyAccs companyAccs) {
        companyService.insertCompanyAccs(companyAccs);
        return success();
    }

    @PostMapping(value = "/updateCompanyPatent")
    public AjaxResult updateCompanyPatent(@RequestBody CompanyPatent companyPatent) {
        companyService.updateCompanyPatent(companyPatent);
        return success();
    }

    @PostMapping(value = "/updateCompanyAccs")
    public AjaxResult updateCompanyAccs(@RequestBody CompanyAccs companyAccs) {
        companyService.updateCompanyAccs(companyAccs);
        return success();
    }

    @PostMapping(value = "/delCompanyPatent")
    public AjaxResult delCompanyPatent(Long id) {
        companyService.delCompanyPatent(id);
        return success();
    }

    @PostMapping(value = "/delCompanyAccs")
    public AjaxResult delCompanyAccs(Long id) {
        companyService.delCompanyAccs(id);
        return success();
    }

    /**
     * 导入企业数据
     *
     * @param file
     * @return
     * @throws Exception
     */
//    @PostMapping("/importData")
//    public AjaxResult importData(MultipartFile file) throws Exception {
//        ExcelUtil<CompanyStd> util = new ExcelUtil<CompanyStd>(CompanyStd.class);
//        List<CompanyStd> companyStdList = util.importExcel(file.getInputStream());
//        companyService.importData(companyStdList);
//        return success();
//    }

    /**
     * 导入企业数据
     *
     * @param file
     * @return
     * @throws Exception
     */
    @Anonymous
    @PostMapping("/importCompanyData")
    public AjaxResult importCompanyData(MultipartFile file) throws Exception {
        ExcelUtil<CompanyExcel> util = new ExcelUtil<CompanyExcel>(CompanyExcel.class);
        List<CompanyExcel> companyExcelList = util.importExcel(file.getInputStream());
        companyService.importCompanyData(companyExcelList);
        return success();
    }

    /**
     * 导入企查查企业数据
     *
     * @param file
     * @return
     * @throws Exception
     */
    @Anonymous
    @PostMapping("/importQccCompanyData")
    public AjaxResult importQccCompanyData(MultipartFile file) throws Exception {
        ExcelUtil<QccCompanyExcel> util = new ExcelUtil<QccCompanyExcel>(QccCompanyExcel.class);
        List<QccCompanyExcel> companyExcelList = util.importExcel(file.getInputStream(),1);
        companyService.importQccCompanyData(companyExcelList);
        return success();
    }


}
